package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dtos.UserSearchDto;
import com.usian.model.search.pojos.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.feign.BehaviorFeign;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        //1.检查数据
        if(dto==null || dto.getPageSize() < 0 || dto.getPageNum() < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        QueryWrapper<ApUserSearch> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUserSearch::getStatus,1).eq(ApUserSearch::getEntryId,apBehaviorEntry.getId());
        Page page = new Page(dto.getPageNum(), dto.getPageSize());
        IPage result = this.page(page,queryWrapper);
        return ResponseResult.okResult(result.getRecords());
    }

    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {
        //1.检查数据
        if(dto==null || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean b = this.update(Wrappers.<ApUserSearch>lambdaUpdate().set(ApUserSearch::getStatus, 0).eq(ApUserSearch::getId, dto.getId()));
        if (b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    @Async("taskExecutor")
    public void insert(Integer entryId, String searchWords) {
        if (entryId==null || StringUtils.isBlank(searchWords)){
            return;
        }
        ApUserSearch apUserSearch = this.getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId)
                .eq(ApUserSearch::getKeyword, searchWords));

        if (apUserSearch!=null){
            if (apUserSearch.getStatus()==0){
                apUserSearch.setStatus(1);
                this.updateById(apUserSearch);
            }else if (apUserSearch.getStatus()==1){
                return;
            }
        }else {
            apUserSearch = new ApUserSearch();
            apUserSearch.setEntryId(entryId);
            apUserSearch.setStatus(1);
            apUserSearch.setKeyword(searchWords);
            apUserSearch.setCreatedTime(new Date());
            save(apUserSearch);
        }
    }
}
